#coding=utf-8
#
# $Id: test.py 2055 2009-11-06 23:09:58Z shodan $
#

from sphinxapi import *
import sys, time

class sphinx:
	q = ''
	mode = SPH_MATCH_ALL
	host = '192.168.1.89'
	port = 9313
	index = '*'
	filtercol = 'group_id'
	filtervals = []
	sortby = ''
	groupby = ''
	groupsort = '@group desc'
	limit = 0
	
	# do query
	cl = SphinxClient()
	cl.SetServer ( host, port )
	cl.SetWeights ( [100, 1] )
	cl.SetMatchMode ( mode )
	if filtervals:
		cl.SetFilter ( filtercol, filtervals )
	if groupby:
		cl.SetGroupBy ( groupby, SPH_GROUPBY_ATTR, groupsort )
	if sortby:
		cl.SetSortMode ( SPH_SORT_EXTENDED, sortby )
	if limit:
		cl.SetLimits ( 0, limit, max(limit,1000) )
	res = cl.Query ( '北京', 'dest_index' )
	
	if not res:
		print 'query failed: %s' % cl.GetLastError()
		sys.exit(1)
	
	if cl.GetLastWarning():
		print 'WARNING: %s\n' % cl.GetLastWarning()
	
	print 'Query \'%s\' retrieved %d of %d matches in %s sec' % (q, res['total'], res['total_found'], res['time'])
	print 'Query stats:'
	
	if res.has_key('words'):
		for info in res['words']:
			print '\t\'%s\' found %d times in %d documents' % (info['word'], info['hits'], info['docs'])
	
	if res.has_key('matches'):
		n = 1
		print '\nMatches:'
		for match in res['matches']:
			attrsdump = ''
			for attr in res['attrs']:
				attrname = attr[0]
				attrtype = attr[1]
				value = match['attrs'][attrname]
				#print attrname+"\t"+attrtype+"\t"+value
				if attrtype==SPH_ATTR_TIMESTAMP:
					value = time.strftime ( '%Y-%m-%d %H:%M:%S', time.localtime(value) )
				attrsdump = '%s, %s=%s' % ( attrsdump, attrname, value )
	
			print '%d. doc_id=%s, weight=%d%s' % (n, match['id'], match['weight'], attrsdump)
			n += 1
